home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / SHLExamples / Delegation / EOFDelegateAdaptorCategory.m < prev    next >
Encoding:
Text File  |  1994-09-14  |  15.0 KB  |  538 lines

  1. /*--------------------------------------------------------------------------
  2.  *
  3.  *     You may freely copy, distribute, and reuse the code in this example.
  4.  *     SHL Systemhouse disclaims any warranty of any kind, expressed or  
  5.  *    implied, as to its fitness for any particular use.
  6.  *
  7.  *
  8.  *    Adaptor
  9.  *
  10.  *    Category Of:    EOFDelegate
  11.  *
  12.  *    Conforms To:    None
  13.  *
  14.  *    Declared In:    EOFDelegateAdaptorCategory.h
  15.  *
  16.  *
  17.  *------------------------------------------------------------------------*/
  18. #import "EOFDelegateAdaptorCategory.h"
  19.  
  20.  
  21.  
  22.  
  23. @implementation EOFDelegate (Adaptor)
  24.  
  25. /*--------------------------------------------------------------------------
  26.  *    EOAdaptor Delegate Methods
  27.  *------------------------------------------------------------------------*/
  28. - (BOOL)adaptor:(EOAdaptor *)adaptor willReportError:(NSString *)error
  29. {
  30.     // An adaptor sends this message to its delegate when an error has
  31.     // occurred.  If the delegate returns NO, the adaptor doesn't open an
  32.     // alert panel (or log the error to the console if it isn't in an
  33.     // application).
  34.     
  35.     BOOL    result = YES;
  36.     
  37.     [[NXApp delegate] announce:adaptor 
  38.         selector:_cmd 
  39.         with:[NSArray arrayWithObject:error]];
  40.     
  41.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  42.         {
  43.         switch (NXRunAlertPanel ("EOAdaptor", 
  44.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  45.             {
  46.             case NX_ALERTALTERNATE:
  47.                 result = NO;
  48.                 break;
  49.             case NX_ALERTDEFAULT:
  50.             default:
  51.                 result = YES;
  52.                 break;
  53.             }
  54.         }
  55.     
  56.     return result;
  57. }
  58.  
  59.  
  60. /*--------------------------------------------------------------------------
  61.  *     EOAdaptorContext Delegate Methods
  62.  *
  63.  *     EOAdaptorContext sends messages to its delegate for any transaction
  64.  *     begin, commit, or rollback. The delegate can use these methods to
  65.  *     preempt these operations, modify their results, or simply track activity.
  66.  *
  67.  *     EODelegateResponse is used to indicate the result a delegate demands
  68.  *     on notification that a particular action will be taken.  If a delegate
  69.  *     returns EODelegateRejects, the sender of the delegation message must
  70.  *     abort the current operation and return a failure result.  If the
  71.  *     delegate returns EODelegateApproves, the sender may continue the
  72.  *     operation and return whatever result is appropriate.  If the delegate
  73.  *     returns EODelegateOverrides, the sender must do nothing, but return a
  74.  *     success result.  When a delegate overrides an operation it's
  75.  *     responsible for seeing that the result of that operation is performed
  76.  *     successfully.
  77.  *
  78.  *     typedef enum { 
  79.  *     EODelegateRejects, EODelegateApproves, EODelegateOverrides
  80.  *     } EODelegateResponse;
  81.  *
  82.  *------------------------------------------------------------------------*/
  83.  
  84. - (EODelegateResponse)adaptorContextWillBegin:context
  85. {
  86.     // Invoked from -beginTransaction to tell the delegate that
  87.     // a transaction is being started.
  88.     
  89.     int    result = EODelegateApproves;
  90.     
  91.     [[NXApp delegate] announce:context selector:_cmd];
  92.     
  93.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  94.         {
  95.         switch (NXRunAlertPanel ("EOAdaptorContext", 
  96.             "%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
  97.             {
  98.             case NX_ALERTOTHER:
  99.                 result = EODelegateOverrides;
  100.                 break;
  101.             case NX_ALERTALTERNATE:
  102.                 result = EODelegateRejects;
  103.                 break;
  104.             case NX_ALERTDEFAULT:
  105.             default:
  106.                 result = EODelegateApproves;
  107.                 break;
  108.             }
  109.         }
  110.     
  111.     return result;
  112. }
  113.  
  114.  
  115. - (void)adaptorContextDidBegin:context
  116. {
  117.     // Invoked from -beginTransaction or -transactionDidBegin to tell the
  118.     // delegate that a transaction has begun. The delegate may take whatever
  119.     // action it needs based on this information.
  120.  
  121.     [[NXApp delegate] announce:context selector:_cmd];
  122. }
  123.  
  124.  
  125. - (EODelegateResponse)adaptorContextWillCommit:context
  126. {
  127.     // Invoked from -commitTransaction to tell the delegate that
  128.     // a transaction is being committed.
  129.     
  130.     int    result = EODelegateApproves;
  131.     
  132.     [[NXApp delegate] announce:context selector:_cmd];
  133.     
  134.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  135.         {
  136.         switch (NXRunAlertPanel ("EOAdaptorContext", 
  137.             "%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
  138.             {
  139.             case NX_ALERTOTHER:
  140.                 result = EODelegateOverrides;
  141.                 break;
  142.             case NX_ALERTALTERNATE:
  143.                 result = EODelegateRejects;
  144.                 break;
  145.             case NX_ALERTDEFAULT:
  146.             default:
  147.                 result = EODelegateApproves;
  148.                 break;
  149.             }
  150.         }
  151.     
  152.     return result;
  153. }
  154.  
  155.  
  156. - (void)adaptorContextDidCommit:context
  157. {
  158.     // Invoked from -commitTransaction or -transactionDidCommit to tell the
  159.     // delegate that a transaction has been committed.  The delegate may take
  160.     // whatever action it needs based on this information.
  161.  
  162.     [[NXApp delegate] announce:context selector:_cmd];
  163. }
  164.  
  165.  
  166. - (EODelegateResponse)adaptorContextWillRollback:context
  167. {
  168.     // Invoked from -rollbackTransaction to tell the delegate that
  169.     // a transaction is being started.
  170.     
  171.     int    result = EODelegateApproves;
  172.     
  173.     [[NXApp delegate] announce:context selector:_cmd];
  174.     
  175.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  176.         {
  177.         switch (NXRunAlertPanel ("EOAdaptorContext", 
  178.             "%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
  179.             {
  180.             case NX_ALERTOTHER:
  181.                 result = EODelegateOverrides;
  182.                 break;
  183.             case NX_ALERTALTERNATE:
  184.                 result = EODelegateRejects;
  185.                 break;
  186.             case NX_ALERTDEFAULT:
  187.             default:
  188.                 result = EODelegateApproves;
  189.                 break;
  190.             }
  191.         }
  192.     
  193.     return result;
  194. }
  195.  
  196.  
  197. - (void)adaptorContextDidRollback:context
  198. {
  199.     // Invoked from -rollbackTransaction or -transactionDidRollback to tell
  200.     // the delegate that a transaction has been rolled back.  The delegate may
  201.     // take whatever action it needs based on this information.
  202.  
  203.     [[NXApp delegate] announce:context selector:_cmd];
  204. }
  205.  
  206.  
  207. /*--------------------------------------------------------------------------
  208.  *     EOAdaptorChannel Delegate Methods
  209.  *
  210.  *  EOAdaptorChannel sends messages to its delegate for nearly every
  211.  *  operation that would affect data in the database server. The delegate can
  212.  *  use these methods to preempt these operations, modify their results,
  213.  *  or simply track activity.
  214.  * 
  215.  *  IMPORTANT: read the comments for the EODelegateResponse type in
  216.  *  EOAdaptorContext.h for information on how the adaptor channel interprets
  217.  *  delegate responses.
  218.  *
  219.  *------------------------------------------------------------------------*/
  220.  
  221. - (EODelegateResponse)adaptorChannel:channel
  222.     willInsertRow:(NSMutableDictionary *)row
  223.     forEntity:(EOEntity *)entity
  224. {
  225.     // Invoked from -insertRow:forEntity: to tell the delegate that a row is
  226.     // being inserted. The delegate may modify row, changing the values for
  227.     // particular keys (attributes), or even adding or deleting attributes.
  228.     
  229.     int    result = EODelegateApproves;
  230.     
  231.     [[NXApp delegate] announce:channel 
  232.         selector:_cmd 
  233.         with:[NSArray arrayWithObjects:row, entity, nil]];
  234.     
  235.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  236.         {
  237.         switch (NXRunAlertPanel ("EOAdaptorChannel", 
  238.             "%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
  239.             {
  240.             case NX_ALERTOTHER:
  241.                 result = EODelegateOverrides;
  242.                 break;
  243.             case NX_ALERTALTERNATE:
  244.                 result = EODelegateRejects;
  245.                 break;
  246.             case NX_ALERTDEFAULT:
  247.             default:
  248.                 result = EODelegateApproves;
  249.                 break;
  250.             }
  251.         }
  252.     
  253.     return result;
  254. }
  255.  
  256.  
  257. - (void)adaptorChannel:channel
  258.     didInsertRow:(NSDictionary *)row
  259.     forEntity:(EOEntity *)entity
  260. {
  261.     // Invoked from -insertRow:forEntity: to tell the delegate that a row has
  262.     // been inserted. The delegate may use this information to update
  263.     // records, redisplay onscreen information, or take whatever other action
  264.     // it needs.
  265.  
  266.     [[NXApp delegate] announce:channel 
  267.         selector:_cmd 
  268.         with:[NSArray arrayWithObjects:row, entity, nil]];
  269. }
  270.  
  271.  
  272. - (EODelegateResponse)adaptorChannel:channel
  273.     willUpdateRow:(NSMutableDictionary *)row
  274.     describedByQualifier:(EOQualifier *)qualifier
  275. {
  276.     // Invoked from -updateRow:describedByQualifier: to tell the delegate
  277.     // that a row is being updated.  The delegate can change the values for
  278.     // particular attribute names in row, add or delete attributes, or modify
  279.     // the qualifier.
  280.     
  281.     int    result = EODelegateApproves;
  282.     
  283.     [[NXApp delegate] announce:channel 
  284.         selector:_cmd 
  285.         with:[NSArray arrayWithObjects:row, qualifier, nil]];
  286.  
  287.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  288.         {
  289.         switch (NXRunAlertPanel ("EOAdaptorChannel", 
  290.             "%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
  291.             {
  292.             case NX_ALERTOTHER:
  293.                 result = EODelegateOverrides;
  294.                 break;
  295.             case NX_ALERTALTERNATE:
  296.                 result = EODelegateRejects;
  297.                 break;
  298.             case NX_ALERTDEFAULT:
  299.             default:
  300.                 result = EODelegateApproves;
  301.                 break;
  302.             }
  303.         }
  304.     
  305.     return result;
  306. }
  307.  
  308.  
  309. - (void)adaptorChannel:channel
  310.     didUpdateRow:(NSDictionary *)row
  311.     describedByQualifier:(EOQualifier *)qualifier
  312. {
  313.     // Invoked from -updateRow:describedByQualifier: to tell the delegate that
  314.     // a row has been updated. The delegate may take whatever action it needs
  315.     // based on this information.
  316.  
  317.     [[NXApp delegate] announce:channel 
  318.         selector:_cmd 
  319.         with:[NSArray arrayWithObjects:row, qualifier, nil]];
  320. }
  321.  
  322.  
  323. - (EODelegateResponse)adaptorChannel:channel
  324.     willDeleteRowsDescribedByQualifier:(EOQualifier *)qualifier
  325. {
  326.     // Invoked from -deleteRowsDescribedByQualifier: to tell the delegate
  327.     // that a row is being deleted.  The delegate can modify the qualifier
  328.     // to affect the result of the delete operation.
  329.     
  330.     int    result = EODelegateApproves;
  331.     
  332.     [[NXApp delegate] announce:channel 
  333.         selector:_cmd 
  334.         with:[NSArray arrayWithObject:qualifier]];
  335.     
  336.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  337.         {
  338.         switch (NXRunAlertPanel ("EOAdaptorChannel", 
  339.             "%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
  340.             {
  341.             case NX_ALERTOTHER:
  342.                 result = EODelegateOverrides;
  343.                 break;
  344.             case NX_ALERTALTERNATE:
  345.                 result = EODelegateRejects;
  346.                 break;
  347.             case NX_ALERTDEFAULT:
  348.             default:
  349.                 result = EODelegateApproves;
  350.                 break;
  351.             }
  352.         }
  353.     
  354.     return result;
  355. }
  356.  
  357.  
  358. - (void)adaptorChannel:channel
  359.     didDeleteRowsDescribedByQualifier:(EOQualifier *)qualifier
  360. {
  361.     // Invoked from -deleteRowsDescribedByQualifier: to tell the delegate that
  362.     // some rows have been deleted. The delegate may take whatever action it
  363.     // needs based on this information.
  364.  
  365.     [[NXApp delegate] announce:channel 
  366.         selector:_cmd 
  367.         with:[NSArray arrayWithObject:qualifier]];
  368. }
  369.  
  370.  
  371. - (EODelegateResponse)adaptorChannel:channel
  372.     willSelectAttributes:(NSMutableArray *)attributes
  373.     describedByQualifier:(EOQualifier *)qualifier
  374.     fetchOrder:(NSMutableArray *)fetchOrder
  375.     lock:(BOOL)flag
  376. {
  377.     // Invoked from -selectAttributes:describedByQualifier:fetchOrder:lock:
  378.     // to tell the delegate that a select operation is being performed.  The
  379.     // delegate can modify attributes or the qualifier to affect the select
  380.     // operation.
  381.     
  382.     int    result = EODelegateApproves;
  383.     
  384.     [[NXApp delegate] announce:channel 
  385.         selector:_cmd 
  386.         with:[NSArray arrayWithObjects: attributes, qualifier, fetchOrder, 
  387.             [NSValue value:&flag withObjCType:"char"], nil]];
  388.     
  389.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  390.         {
  391.         switch (NXRunAlertPanel ("EOAdaptorChannel", 
  392.             "%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
  393.             {
  394.             case NX_ALERTOTHER:
  395.                 result = EODelegateOverrides;
  396.                 break;
  397.             case NX_ALERTALTERNATE:
  398.                 result = EODelegateRejects;
  399.                 break;
  400.             case NX_ALERTDEFAULT:
  401.             default:
  402.                 result = EODelegateApproves;
  403.                 break;
  404.             }
  405.         }
  406.     
  407.     return result;
  408. }
  409.     
  410.  
  411. - (void)adaptorChannel:channel
  412.     didSelectAttributes:(NSArray *)attributes
  413.     describedByQualifier:(EOQualifier *)qualifier
  414.     fetchOrder:(NSArray *)fetchOrder
  415.     lock:(BOOL)flag
  416. {
  417.     // Invoked from -selectAttributes:describedByQualifier:fetchOrder:lock:
  418.     // to tell the delegate that some rows have been selected. The delegate
  419.     // may take whatever action it needs based on this information.
  420.     
  421.     [[NXApp delegate] announce:channel 
  422.         selector:_cmd 
  423.         with:[NSArray arrayWithObjects: attributes, qualifier, fetchOrder, 
  424.             [NSValue value:&flag withObjCType:"char"], nil]];
  425. }
  426.     
  427.  
  428. - (NSMutableDictionary *)adaptorChannel:channel
  429.     willFetchAttributes:(NSMutableArray *)attributes
  430.     withZone:(NSZone *)zone
  431. {
  432.     // Invoked from -fetchAttributes:withZone: to tell the delegate that a
  433.     // single row will be fetched. The delegate may modify attributes by
  434.     // adding or deleting attributes. If the delegate returns a non-nil
  435.     // value, that value will be treated as a dictionary resulting from a
  436.     // fetch operation; the adaptor channel doesn't perform a fetch, and
  437.     // -fetchAttributes:withZone: returns YES. If the delegate returns nil
  438.     // the adaptor channel perfoms the fetch itself.
  439.     
  440.     [[NXApp delegate] announce:channel 
  441.         selector:_cmd 
  442.         with:[NSArray arrayWithObject:attributes]];
  443.  
  444.     return nil;
  445. }
  446.  
  447.  
  448. - (NSMutableDictionary *)adaptorChannel:channel
  449.     didFetchAttributes:(NSDictionary *)attributes
  450.     withZone:(NSZone *)zone
  451. {
  452.     // Invoked from -fetchAttributes:withZone: to tell the delegate that a
  453.     // single row has been fetched.  The delegate may modify and return the
  454.     // given dictionary of attributes, return a substitute dictionary, or nil
  455.     // to cause -fetchAttributes:withZone: to fail and return NO.
  456.     
  457.     [[NXApp delegate] announce:channel 
  458.         selector:_cmd 
  459.         with:[NSArray arrayWithObject:attributes]];
  460.  
  461.     return (NSMutableDictionary *)attributes;
  462. }
  463.  
  464.  
  465. - (void)adaptorChannelDidChangeResultSet:channel
  466. {
  467.     // Invoked from -fetchAttributes:withZone: to tell the delegate that
  468.     // fetching will start for the next result set, when a select operation
  469.     // resulted in multiple result sets.  This method is invoked just after a
  470.     // -fetchAttributes:withZone: returns nil when there are still result sets
  471.     // left to fetch.
  472.  
  473.     [[NXApp delegate] announce:channel selector:_cmd];
  474. }
  475.  
  476.  
  477. - (void)adaptorChannelDidFinishFetching:channel
  478. {
  479.     // Invoked from -fetchAttributes:withZone: to tell the delegate that
  480.     // fetching is finished for the current select operation.  This method is
  481.     // invoked when a fetch ends in -fetchAttributes:withZone: because there
  482.     // are no more result sets.
  483.  
  484.     [[NXApp delegate] announce:channel selector:_cmd];
  485. }
  486.  
  487.  
  488. - (EODelegateResponse)adaptorChannel:channel 
  489.     willEvaluateExpression:(NSMutableString *)expression
  490. {
  491.     // Invoked from -evaluateExpression: to tell the delegate that an
  492.     // expression is about to be sent to the database server. The delegate
  493.     // may modify expression to affect the result.
  494.  
  495.     int    result = EODelegateApproves;
  496.  
  497.     [[NXApp delegate] announce:channel 
  498.         selector:_cmd 
  499.         with:[NSArray arrayWithObject:expression]];
  500.  
  501.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  502.         {
  503.         switch (NXRunAlertPanel ("EOAdaptorChannel", 
  504.             "%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
  505.             {
  506.             case NX_ALERTOTHER:
  507.                 result = EODelegateOverrides;
  508.                 break;
  509.             case NX_ALERTALTERNATE:
  510.                 result = EODelegateRejects;
  511.                 break;
  512.             case NX_ALERTDEFAULT:
  513.             default:
  514.                 result = EODelegateApproves;
  515.                 break;
  516.             }
  517.         }
  518.     
  519.     return result;
  520. }
  521.  
  522.  
  523. - (void)adaptorChannel:channel
  524.     didEvaluateExpression:(NSString *)expression
  525. {
  526.     // Invoked from -evaluateExpression: to tell the delegate that a query
  527.     // language expression has been evaluated by the database server. The
  528.     // delegate may take whatever action it needs based on this information.
  529.  
  530.     [[NXApp delegate] announce:channel 
  531.         selector:_cmd 
  532.         with:[NSArray arrayWithObject:expression]];
  533. }
  534.  
  535.  
  536. @end
  537.  
  538.